// 云函数入口文件
const cloud = require('wx-server-sdk')
cloud.init()

const db = cloud.database()
const _ = db.command

exports.main = async (event, context) => {
  const query = event.query.toLowerCase()
  try {
    // 获取数据库中所有的股票数据
    const res = await db.collection('allStock').get()
    const allItems = res.data[0].data.items
    
    // 过滤出与查询字符串匹配的项
    const filteredItems = allItems.filter(item => item[1].toLowerCase().includes(query))

    // 只返回前30个匹配的项
    const limitedItems = filteredItems.slice(0, 30)

    return {
      data: limitedItems
    }
  } catch (e) {
    console.error(e)
    return {
      data: []
    }
  }
}
